*********************************************************************************
*                                                                               *
*                Election Frequency - Preparing the Dataset     	            *
*                                                                               *
*                     Dealing with Simultaneous Elections                       *
*                                                                               *
*********************************************************************************

global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'  // F - Home

cd `"${beg_path}Data\Analysis 1\"'
use electionsdata.dta, clear


/* Identifying simultaneous elections */
by date Country, sort: gen nvals = _n == 1
/* nvals is 1 whenever a value is first in its group and is 0 otherwise */
count if nvals == 0     // total number of pairs
tab nvals Country
sort Country date 
list Country Type Year Date date nvals


/* creating groups */
* Generate groups of elections by country and date
egen Group = group(date Country) 
tab Group

* Rank based on the election-type hierarchy
generate Rank1 = Type
recode Rank1 (1 = 2) (2 = 1) (3 = 3) (4 = 7) (5 = 8) (6 = 6) (7 = 5) (8 = 4) (10 = 9), generate(Rank)
drop Rank1

* Rank within a group by Type and Coverage
generate Coverage_pop2 = 1/Coverage_pop  // preparing to sort
sort Group Coverage_pop2 Rank 		 // sort by Group, Coverage (inversed) and Rank	
quietly by Group :  gen Rank2 = cond(_N==1,0,_n)
recode Rank2 (0 =.), generate (Rankgroup) 
drop Rank2 

* labels 
label var Group "Groups by election date and country"
label var Rank "Rank based one the election-type hierarchy"
label var Coverage_pop2 "Inverted population coverage to be used in sort"
label var Rankgroup "Rank within the group by pop coverage and rank"
  
/* Joint elections */

* Re-grouping joint elections
sum Rankgroup // max 4
gen joint_el1 = Type[_n+1] if Rankgroup == 1&Rankgroup[_n+1] == 2&Group == Group[_n+1]
gen joint_el2 = Type[_n+2] if Rankgroup == 1&Rankgroup[_n+2] == 3&Group == Group[_n+2]
gen joint_el3 = Type[_n+3] if Rankgroup == 1&Rankgroup[_n+3] == 4&Group == Group[_n+3]

* Labels 
label var joint_el1 "2nd simultaneous elections"
label var joint_el2 "3rd simultaneous elections"
label var joint_el3 "4th simultaneous elections"

* Delete 2nd and later elections 
drop if Rankgroup > 1 & Rankgroup != .

* Creating dummies for joint elections 
forvalues i=1(1)10  { 
generate Joint`i' = 1 if `i' == joint_el1 | `i' == joint_el2 | `i' == joint_el3  
}
foreach x of varlist Joint1-Joint10 {
replace `x' = 0 if (`x' >= .)
}
drop Joint9    // no type 9

* Labels
label var Joint1 "Joint Presidential"
label var Joint2 "Joint Lowerhouse"
label var Joint3 "Joint Upperhouse"
label var Joint4 "Joint Referendum"
label var Joint5 "Joint European"
label var Joint6 "Joint Regional"
label var Joint7 "Joint Local"
label var Joint8 "Joint Municipal"
label var Joint10 "Joint Others"

save electionsdata_2.dta, replace



